package make

import (
	"github.com/spf13/cobra"
	"os"
)

var CmdModel = &cobra.Command{
	Use:   "model",
	Short: "HERE PUTS THE COMMAND DESCRIPTION",
	Run:   runModel,
}
var (
	table string
	dao   string
)

func init() {
	CmdModel.MarkFlagRequired("table")
	CmdModel.MarkFlagRequired("dao")
	CmdModel.Flags().StringVarP(&table, "table", "m", "", "make model")
	CmdModel.Flags().StringVarP(&dao, "dao", "d", "", "make dao")
}

// go run main.go --env=local make model --table=project  --dao=card
func runModel(cmd *cobra.Command, args []string) {

	// 格式化模型名称，返回一个 Model 对象
	model := makeModelFromString([]string{table, dao}...)
	// 确保模型的目录存在，例如 `app/models/user`
	dir := "app/models/"
	// os.MkdirAll 会确保父目录和子目录都会创建，第二个参数是目录权限，使用 0777
	os.MkdirAll(dir, os.ModePerm)

	// 替换变量
	createFileFromStub(dir+model.PackageName+"_model.go", "model", model)
}
